package com.hzqy.service.publicize;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.content.ContentDao;
import com.hzqy.persistence.dao.pms.PmsDao;
import com.hzqy.persistence.dao.publicize.PublicizeDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmUserOrgVo;
import com.hzqy.web.vo.PmsOrgVo;

@Service
public class PublicizeServiceBean extends GenericServiceBean<CmContentVo, ContentDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private ContentDao contentDao;
	@Resource
	private PublicizeDao publicizeDao;
	@Resource
	private PmsDao pmsDao;
	
	@Override
	public ContentDao getEntityDao(){
		return contentDao;
	}
	
	public Map<String,Object> selectUserOrgList(CmUserOrgVo cmUserOrgVo,int curPage,
			int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_cuo_status", cmUserOrgVo.getF_cuo_status());
		map.put("f_po_id", cmUserOrgVo.getF_po_id());
		map.put("f_cuo_orgId", cmUserOrgVo.getF_cuo_orgId());
		map.put("f_cuo_userAccount",cmUserOrgVo.getF_cuo_userAccount());
		
		Integer rowCount = publicizeDao.findUserOrgCount(cmUserOrgVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			
			List<CmUserOrgVo> results = publicizeDao.selectUserOrgList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmUserOrgVo>());
		}
		return map;
	}
	
	public int insertUserOrg(CmUserOrgVo cmUserOrgVo) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(cmUserOrgVo.getF_cuo_onlinedate()!=""&&cmUserOrgVo.getF_cuo_onlinedate()!=null){
			try {
				Date date = sdf.parse(cmUserOrgVo.getF_cuo_onlinedate());
				Calendar calendar1 = Calendar.getInstance();
				Calendar calendar2 = Calendar.getInstance();
				calendar1.setTime(date);
				calendar2.setTime(date);
				calendar2.add(Calendar.DATE,cmUserOrgVo.getF_cuo_contribute_validDate());
				String f_cuo_onlinedate = sdf.format(calendar1.getTime());
				String f_cuo_offlinedate = sdf.format(calendar2.getTime());
				cmUserOrgVo.setF_cuo_onlinedate(f_cuo_onlinedate);
				cmUserOrgVo.setF_cuo_offlinedate(f_cuo_offlinedate);
			} catch (ParseException e) {
				LOG.error("投放时间转化出错："+e);
			}
		}else{//默认生效时间为当前时间
			Calendar calendar1 = Calendar.getInstance();
			Calendar calendar2 = Calendar.getInstance();
			calendar2.add(Calendar.DATE,cmUserOrgVo.getF_cuo_contribute_validDate());
			String f_cuo_onlinedate = sdf.format(calendar1.getTime());
			String f_cuo_offlinedate = sdf.format(calendar2.getTime());
			cmUserOrgVo.setF_cuo_onlinedate(f_cuo_onlinedate);
			cmUserOrgVo.setF_cuo_offlinedate(f_cuo_offlinedate);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("firstResult", 0);
		map.put("maxResults", 1);
		map.put("f_cc_name", cmUserOrgVo.getF_cuo_userAccount());
		List<CmContentVo> contentList = publicizeDao.selectContentListByName(map);
		if(contentList.size()>0){
			cmUserOrgVo.setF_po_id(contentList.get(0).getOrgId());
			cmUserOrgVo.setF_po_name(contentList.get(0).getF_cc_companyname());
		}
		return publicizeDao.insertUserOrg(cmUserOrgVo);
	}
	
	public int deleteUserOrg(int f_cuo_id) {
		publicizeDao.deleteUserOrg(f_cuo_id);
		return 1;
	}
	
	public int batchDeleteUserOrg(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			flag += publicizeDao.deleteUserOrg(Integer.valueOf(idsArr[i]));
		}
		return flag;
	}
	
	public Map<String,Object> batchContribute(CmUserOrgVo cmUserOrgVo,int type) {
		Map<String,Object> resultMap = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE,cmUserOrgVo.getF_cuo_contribute_validDate());
		String f_cuo_onlinedate = sdf.format(calendar1.getTime());
		String f_cuo_offlinedate = sdf.format(calendar2.getTime());

		int f_cuo_orgId = cmUserOrgVo.getF_cuo_orgId();
		PmsOrgVo pmsOrgVo= pmsDao.selectOrg(f_cuo_orgId);
		double f_po_lon = pmsOrgVo.getF_po_lon();
		double f_po_lat = pmsOrgVo.getF_po_lat();
		int contribute_distance = cmUserOrgVo.getContribute_distance();
		int contribute_num = cmUserOrgVo.getContribute_num();
		int contribute_type = cmUserOrgVo.getContribute_type();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_po_lon", f_po_lon);
		map.put("f_po_lat",f_po_lat);
		map.put("contribute_distance",contribute_distance);
		map.put("f_cuo_orgId",f_cuo_orgId);
		List<PmsOrgVo> list = publicizeDao.selectOrgByCoordinateCount(map);
		StringBuffer str = new StringBuffer();
		List<HashMap<String,Object>> contributeList = new ArrayList<HashMap<String,Object>>();
		if(contribute_type==1){//由近及远
			for (PmsOrgVo pmsOrg : list) {
				int user_count = pmsOrg.getUser_count();
				if(contribute_num<=user_count){
					str.append(pmsOrg.getF_po_name()+"投放"+contribute_num+"个;");
					HashMap<String, Object> contributeMap = new HashMap<String,Object>();
					contributeMap.put("firstResult", 0);
					contributeMap.put("maxResults", contribute_num);
					contributeMap.put("f_po_id", pmsOrg.getF_po_id());
					contributeMap.put("f_cuo_orgId", f_cuo_orgId);
					contributeList.add(contributeMap);
					contribute_num = 0;
					break;
				}else{
					str.append(pmsOrg.getF_po_name()+"投放"+user_count+"个;");
					HashMap<String, Object> contributeMap = new HashMap<String,Object>();
					contributeMap.put("firstResult", 0);
					contributeMap.put("maxResults", user_count);
					contributeMap.put("f_po_id", pmsOrg.getF_po_id());
					contributeMap.put("f_cuo_orgId", f_cuo_orgId);
					contributeList.add(contributeMap);
					contribute_num = contribute_num - user_count;
				}
			}
			str.append("剩余投放"+contribute_num+"个;");
		}else if(contribute_type==2){//平均投放
			if(list.size()>0){
				int orgCount = list.size();
				int avg = contribute_num/orgCount;
				for (PmsOrgVo pmsOrg : list) {
					int user_count = pmsOrg.getUser_count();
					if(avg<=user_count){
						str.append(pmsOrg.getF_po_name()+"投放"+avg+"个;");
						HashMap<String, Object> contributeMap = new HashMap<String,Object>();
						contributeMap.put("firstResult", 0);
						contributeMap.put("maxResults", avg);
						contributeMap.put("f_po_id", pmsOrg.getF_po_id());
						contributeMap.put("f_cuo_orgId", f_cuo_orgId);
						contributeList.add(contributeMap);
						contribute_num = contribute_num - avg;
					}else{
						str.append(pmsOrg.getF_po_name()+"投放"+user_count+"个;");
						HashMap<String, Object> contributeMap = new HashMap<String,Object>();
						contributeMap.put("firstResult", 0);
						contributeMap.put("maxResults", user_count);
						contributeMap.put("f_po_id", pmsOrg.getF_po_id());
						contributeMap.put("f_cuo_orgId", f_cuo_orgId);
						contributeList.add(contributeMap);
						contribute_num = contribute_num - user_count;
					}
				}
			}
			str.append("未投放个数："+contribute_num+"个;");
		}
		resultMap.put("success", true);
		if(type==1){//预览
			if("".equals(str.toString())){
				resultMap.put("result", "结果预览：无");
			}else{
				resultMap.put("result", "结果预览："+str.toString());
			}
		}else if(type==2){//投放
			resultMap.put("result", "投放结果："+str.toString());
			for (HashMap<String, Object> hashMap : contributeList) {
				List<CmContentVo> contentList = publicizeDao.selectContentListByOrgId(hashMap);
				for(int i = 0;i<contentList.size();i++){
					CmUserOrgVo cmUserOrg = new CmUserOrgVo();
					cmUserOrg.setF_cuo_onlinedate(f_cuo_onlinedate);
					cmUserOrg.setF_cuo_offlinedate(f_cuo_offlinedate);
					cmUserOrg.setF_cuo_orgId(f_cuo_orgId);
					cmUserOrg.setF_cuo_userAccount(contentList.get(i).getF_cc_name());
					cmUserOrg.setF_po_id(contentList.get(i).getOrgId());
					cmUserOrg.setF_po_name(contentList.get(i).getF_cc_companyname());
					cmUserOrg.setF_cuo_contribute_validDate(cmUserOrgVo.getF_cuo_contribute_validDate());
					publicizeDao.insertUserOrg(cmUserOrg);
				}
			}
		}
		return resultMap;
	}
	
	public List<PmsOrgVo> selectOrgByCoordinate(double f_po_lon,double f_po_lat,int contribute_distance){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_po_lon", f_po_lon);
		map.put("f_po_lat",f_po_lat);
		map.put("contribute_distance",contribute_distance);
		List<PmsOrgVo> list = publicizeDao.selectOrgByCoordinate(map);
		return list;
	}
	
	public Map<String,Object> selectContributeOrgList(CmUserOrgVo cmUserOrgVo, int curPage, int pageSize){
		int f_cuo_orgId = cmUserOrgVo.getF_cuo_orgId();
		if(f_cuo_orgId==0){
			return null;
		}
		Map<String,Object> resultMap = new HashMap<String, Object>();
		PmsOrgVo pmsOrgVo= pmsDao.selectOrg(f_cuo_orgId);
		double f_po_lon = pmsOrgVo.getF_po_lon();
		double f_po_lat = pmsOrgVo.getF_po_lat();
		int contribute_distance = 0;
		Map<String,Object> hashMap = new HashMap<String, Object>();
		hashMap.put("f_po_lon", f_po_lon);
		hashMap.put("f_po_lat",f_po_lat);
		hashMap.put("contribute_distance",contribute_distance);
		hashMap.put("f_cuo_orgId",f_cuo_orgId);
		Integer rowCount = publicizeDao.selectContributeOrgListCount(hashMap);
		resultMap.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_po_lon", f_po_lon);
			map.put("f_po_lat",f_po_lat);
			map.put("contribute_distance",contribute_distance);
			map.put("f_cuo_orgId",f_cuo_orgId);
			List<PmsOrgVo> list = publicizeDao.selectOrgByCoordinateCount(map);
			resultMap.put("rows", list);
		} else {
			resultMap.put("rows", new ArrayList<PmsOrgVo>());
		}
		return resultMap;
	}
	
	public Map<String,Object> batchContributeByOrg(CmUserOrgVo cmUserOrgVo) {
		Map<String,Object> resultMap = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE,cmUserOrgVo.getF_cuo_contribute_validDate());
		String f_cuo_onlinedate = sdf.format(calendar1.getTime());
		String f_cuo_offlinedate = sdf.format(calendar2.getTime());
		int contribute_num = cmUserOrgVo.getContribute_num();
		
		HashMap<String, Object> hashMap = new HashMap<String,Object>();
		hashMap.put("firstResult", 0);
		hashMap.put("maxResults", contribute_num);
		hashMap.put("f_po_id", cmUserOrgVo.getF_po_id());
		hashMap.put("f_cuo_orgId", cmUserOrgVo.getF_cuo_orgId());
		List<CmContentVo> contentList = publicizeDao.selectContentListByOrgId(hashMap);
		
		for(int i = 0;i<contentList.size();i++){
			CmUserOrgVo cmUserOrg = new CmUserOrgVo();
			cmUserOrg.setF_cuo_onlinedate(f_cuo_onlinedate);
			cmUserOrg.setF_cuo_offlinedate(f_cuo_offlinedate);
			cmUserOrg.setF_cuo_orgId(cmUserOrgVo.getF_cuo_orgId());
			cmUserOrg.setF_cuo_userAccount(contentList.get(i).getF_cc_name());
			cmUserOrg.setF_po_id(contentList.get(i).getOrgId());
			cmUserOrg.setF_po_name(contentList.get(i).getF_cc_companyname());
			cmUserOrg.setF_cuo_contribute_validDate(cmUserOrgVo.getF_cuo_contribute_validDate());
			publicizeDao.insertUserOrg(cmUserOrg);
		}
		
		resultMap.put("success", true);
		resultMap.put("result", "投放成功"+contentList.size()+"条");
		return resultMap;
	}
	
	public List<CmUserOrgVo> selectOrgByOrgids(CmUserOrgVo cmUserOrgVo , String orgids) {
		HashMap<String, Object> hashMap = new HashMap<String,Object>();
		hashMap.put("f_cuo_orgId", cmUserOrgVo.getF_cuo_orgId());
		hashMap.put("orgids", orgids);
		hashMap.put("f_cuo_status", cmUserOrgVo.getF_cuo_status());
		List<CmUserOrgVo> list = publicizeDao.selectOrgByOrgids(hashMap);
		return list;
	}
}
